package vn.com.vtv.weather.adapter;

import org.openweathermap.model.Cities;

import vn.com.vtv.weather.VTVApplication;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CityAdapter extends ArrayAdapter<Cities>{
	Cities[] cities;
	public CityAdapter(Context context, Cities[] objects) {
		super(context,android.R.layout.simple_spinner_item);
		cities = objects;
	}
	@Override
	public int getCount() {
		return cities.length;
	}
	@Override
	public Cities getItem(int position) {
		return cities[position];
	}
	@Override
	public long getItemId(int position) {
		return cities[position].id;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_spinner_item, null);
		Cities city = cities[position];
		TextView tv = (TextView)convertView.findViewById(android.R.id.text1);
		tv.setText(city.name);
		return convertView;
	}
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		TextView label = new TextView(getContext());
		if(VTVApplication.prefs.getInt(VTVApplication.KEY_CITY_ID, VTVApplication.KEY_CITY_ID_DEF) == cities[position].id){
			label.setTextColor(Color.RED);
		}else{
			label.setTextColor(Color.BLACK);
		}
        
        label.setText(cities[position].name);
		return label;
	}
}
